#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char const *argv[])
{
    int count = 100; //定义一个全局变量

    pid_t pid = fork(); //错误
    if (pid < 0)
    {
        perror("fork");
        exit(-1);
    }
    else if (pid == 0) //表示子进程，子进程调用myls程序
    {
        printf("child: pid=%d\n", getpid());  //获取当进程的进程号
        printf("child: pid=%d\n", getppid()); //获取父进程的进程号
        execlp("ls", "ls", "-l", "/home/linux",NULL);
        exit(0);
    }
    else //表示父进程 pid>0
    {
        printf("parent: pid=%d\n", getpid());  //获取当进程的进程号
        printf("parent: pid=%d\n", getppid()); //获取父进程的进程号

        while (1)
        {
            count--;
            printf("parent: procrss is running:%d\n", count);
            sleep(1);
        }
    }

    return 0;
}
